/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: HYF
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using FriendStar.Code;
using FriendStar.Data;
using FriendStar.Domain.Entity.WareHouse;
using FriendStar.Domain.IRepository.WareHouse;
using FriendStar.Repository.Infrastructure;
using System;
using System.Collections.Generic;

namespace FriendStar.Repository.WareHouse
{
    public class CPK_31_BL_StoArcPreMaterialRepository : RepositoryBase<CPK_31_BL_StoArcPreMaterialEntity>, ICPK_31_BL_StoArcPreMaterialRepository
    {
        public void SavePrepare(List<CPK_31_BL_StoArcPreMaterialEntity> list)
        {
            string UseUnit = Configs.GetValue("UseUnit");
            string UserNO = OperatorProvider.Provider.GetCurrent().UserCode;
            string UserName= OperatorProvider.Provider.GetCurrent().UserName;
            string FFacNum = Configs.GetValue("FactoryFlag");
            string DocNum = GetDocumentNum.GetNum(UseUnit, FFacNum, "台账日期成品", UserNO, "", "临时");
            using (var db = new RepositoryBase().BeginTrans())
            {
                try
                {
                    #region 执行
                    string userCode = OperatorProvider.Provider.GetCurrent().UserCode;
                    db.FindList<string>("delete from CPK_31_BL_StoArcPreMaterial_ZJ where  FUserNO=@FUserNO", new { FUserNO= userCode });
                    foreach (CPK_31_BL_StoArcPreMaterialEntity item in list)
                    {
                        var obj = new
                        {
                            UseUnit = UseUnit,
                            FRegisterID = item.FRegisterID,
                            FUserNO = userCode,
                            FCurTime = DateTime.Now.ToString("HH:mm:ss")
                        };
                        db.FindList<string>("insert into CPK_31_BL_StoArcPreMaterial_ZJ (UseUnit,FRegisterID,FUserNO,FCurTime) values(@UseUnit,@FRegisterID,@FUserNO,@FCurTime)", obj);
                    }
                    string FCurTime = DateTime.Now.ToString("HH:mm:ss");
                    db.ExecuteStoredProcedure<CPK_31_BL_StoArcPreMaterialEntity>("InOutStorage_UpdatePreNO_New", new { UseUnit = UseUnit, FFacNum= FFacNum, UserNO= UserNO, FAccountDate= DocNum, FCreateBy=UserName, FCurTime= FCurTime });
                    #endregion 执行
                }
                catch (System.Exception ex)
                {
                    db.RollBack();
                    throw ex;
                }
            }
        }

        public void EditPrepare(CPK_31_BL_StoArcPreMaterialEntity data)
        {
            string UseUnit = Configs.GetValue("UseUnit");
            string UserNO = OperatorProvider.Provider.GetCurrent().UserCode;
            string UserName = OperatorProvider.Provider.GetCurrent().UserName;
            string FFacNum = Configs.GetValue("FactoryFlag");
            CPK_31_BL_StoArcPreMaterialEntity oldData = FindEntity(p => p.FPreMaterialID == data.FPreMaterialID);
            using (var db = new RepositoryBase().BeginTrans())
            {
                try
                {
                    #region 执行
                    db.FindList<string>("update CPK_61_PC_StoCPKAIAOAcco  set FQuantity_CK=FQuantity_CK-@FQuantity) where FInOutID =@FRegisterID_ZL", new { FQuantity = oldData.FQuantity, FRegisterID_ZL = oldData.FRegisterID_ZL });

                    db.FindList<string>("update CPK_61_PC_StoCPKAIAOAcco  set FQuantity_CK=FQuantity_CK+@FQuantity) where FInOutID =@FRegisterID_ZL", new { FQuantity = data.FQuantity, FRegisterID_ZL = data.FRegisterID_ZL });


                    CPK_71_KC_StoCPKStockCataEntity StoCPKStockCata =db.FindEntity<CPK_71_KC_StoCPKStockCataEntity>(p => p.FProCompanyNO == data.FProCompanyNO && p.FProductionNO == data.FProductionNO && p.FProductionTypeNO == data.FProductionTypeNO && p.UseUnit == UseUnit);

                    StoCPKStockCata.FImmeQuantity = StoCPKStockCata.FImmeQuantity + oldData.FQuantity - data.FQuantity;

                    db.Update<CPK_71_KC_StoCPKStockCataEntity>(new { StoCPKStockCata.FImmeQuantity }, p => p.FProCompanyNO == data.FProCompanyNO && p.FProductionNO == data.FProductionNO && p.FProductionTypeNO == data.FProductionTypeNO && p.UseUnit == UseUnit);


                    db.Update<CPK_31_BL_StoArcPreMaterialEntity>(new {data.FQuantity,data.FRegisterID_ZL }, p => p.FPreMaterialID == data.FPreMaterialID);

                    #endregion 执行
                }
                catch (System.Exception ex)
                {
                    db.RollBack();
                    throw ex;
                }
            }
        }

        public void SavePrepareToOut(List<CPK_31_BL_StoArcPreMaterialEntity> list)
        {
            string UseUnit = Configs.GetValue("UseUnit");
            string UserNO = OperatorProvider.Provider.GetCurrent().UserCode;
            string UserName = OperatorProvider.Provider.GetCurrent().UserName;
            string FFacNum = Configs.GetValue("FactoryFlag");
            string DocNum = GetDocumentNum.GetNum(UseUnit, FFacNum, "台账日期成品", UserNO, "", "临时");
            using (var db = new RepositoryBase().BeginTrans())
            {
                try
                {
                    #region 执行
                    foreach (CPK_31_BL_StoArcPreMaterialEntity item in list)
                    {
                        db.ExecuteStoredProcedure<CPK_31_BL_StoArcPreMaterialEntity>("Pro_StoCPKListAcco", new { FPreMaterialID = item.FPreMaterialID, FAuditingBy = UserName });
                    }

                    db.ExecuteStoredProcedure<CPK_31_BL_StoArcPreMaterialEntity>("Pro_StoPreMaterial", new { UseUnit = UseUnit, FFacNum = FFacNum, UserNO = UserNO, FAccountDate = DocNum, @UserName = UserName, FDocuTypeNO="CC" });
                    #endregion 执行
                }
                catch (System.Exception ex)
                {
                    db.RollBack();
                    throw ex;
                }
            }
        }
    }
}
